package sopt.dev.network;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import com.google.gson.Gson;

public class SendItemRequest extends NetworkRequest {

        private final String SERVER_IP = "http://163.239.200.121:8080";
        private HttpURLConnection conn;
        private Object sendObj;

        public SendItemRequest( Object sendObj )  {
                
                        this.sendObj = sendObj;
                        setUrl(SERVER_IP);              
        }
                

        @Override
        protected void parsing() {
                
                try {
                        
                        conn = (HttpURLConnection) url.openConnection();
                        conn.setDoOutput(true);
                        conn.setRequestMethod("POST");
                        conn.setRequestProperty( "Content-Type", "application/json" );
                        
                        OutputStream os = conn.getOutputStream();
                        os.write( new Gson().toJson(sendObj).getBytes() );
                        os.flush();
                 
                        BufferedReader br = new BufferedReader
                                        (new InputStreamReader(( conn.getInputStream() )));
         
                        StringBuilder response = new StringBuilder();
                        String line;
                        while ( (line = br.readLine() ) != null) {
                                response.append(line);
                        }
         
                        result = response.toString();
                        
                        br.close();
                        conn.disconnect();

                        
                } catch (IOException e) {
                        e.printStackTrace();
                }
                
        }

}